//
//  ONISOObjects.m
//
//  Created by   on 6/5/14
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ONISOObjects.h"


NSString *const kONISOObjectsCreationDate = @"creationDate";
NSString *const kONISOObjectsCreationName = @"creationName";
NSString *const kONISOObjectsId = @"id";
NSString *const kONISOObjectsName = @"name";
NSString *const kONISOObjectsDescription = @"description";
NSString *const kONISOObjectsHasChildren = @"hasChildren";

/// more not in API
NSString *const kONISOObjectsparentISO = @"parentISO";
NSString *const kONISOObjectsparentLoadZone = @"parentLoadZone";
/// int
NSString *const kONISOObjectstype = @"OBJECT_TYPE";
NSString *const kONISOObjectAdd = @"OBJECT_ADD";

@interface ONISOObjects ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ONISOObjects

@synthesize creationDate = _creationDate;
@synthesize creationName = _creationName;
@synthesize objectsIdentifier = _objectsIdentifier;
@synthesize name = _name;
@synthesize objectsDescription = _objectsDescription;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    BOOL isDictionnary=false;
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        isDictionnary=true;
            self.creationDate = [self objectOrNilForKey:kONISOObjectsCreationDate fromDictionary:dict];
            self.creationName = [self objectOrNilForKey:kONISOObjectsCreationName fromDictionary:dict];
            self.objectsIdentifier = [[self objectOrNilForKey:kONISOObjectsId fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kONISOObjectsName fromDictionary:dict];
            self.objectsDescription = [self objectOrNilForKey:kONISOObjectsDescription fromDictionary:dict];
            self.hasChild=[[self objectOrNilForKey:kONISOObjectsHasChildren fromDictionary:dict] boolValue];
        
        
    }
    
    return (isDictionnary)?self:nil;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.creationDate forKey:kONISOObjectsCreationDate];
    [mutableDict setValue:self.creationName forKey:kONISOObjectsCreationName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.objectsIdentifier] forKey:kONISOObjectsId];
    [mutableDict setValue:self.name forKey:kONISOObjectsName];
    [mutableDict setValue:self.objectsDescription forKey:kONISOObjectsDescription];
    [mutableDict setValue:[NSNumber numberWithBool:self.hasChild] forKeyPath:kONISOObjectsHasChildren];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.creationDate = [aDecoder decodeObjectForKey:kONISOObjectsCreationDate];
    self.creationName = [aDecoder decodeObjectForKey:kONISOObjectsCreationName];
    self.objectsIdentifier = [aDecoder decodeDoubleForKey:kONISOObjectsId];
    self.name = [aDecoder decodeObjectForKey:kONISOObjectsName];
    self.objectsDescription = [aDecoder decodeObjectForKey:kONISOObjectsDescription];
    self.hasChild = [aDecoder decodeBoolForKey:kONISOObjectsHasChildren];
    
    //more
     self.iso =  [aDecoder decodeObjectForKey:kONISOObjectsparentISO];
     self.loadZone =[aDecoder decodeObjectForKey:kONISOObjectsparentLoadZone];
     self.type=[aDecoder decodeIntForKey:kONISOObjectstype];
     self.isAdded=[aDecoder decodeFloatForKey:kONISOObjectAdd];
    
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_creationDate forKey:kONISOObjectsCreationDate];
    [aCoder encodeObject:_creationName forKey:kONISOObjectsCreationName];
    [aCoder encodeDouble:_objectsIdentifier forKey:kONISOObjectsId];
    [aCoder encodeObject:_name forKey:kONISOObjectsName];
    [aCoder encodeObject:_objectsDescription forKey:kONISOObjectsDescription];
    [aCoder encodeBool:self.hasChild forKey:kONISOObjectsHasChildren];
    
    /// more
    [aCoder encodeObject:self.iso forKey:kONISOObjectsparentISO];
    [aCoder encodeObject:self.loadZone forKey:kONISOObjectsparentLoadZone];
    [aCoder encodeInt:self.type forKey:kONISOObjectstype];
    [aCoder  encodeFloat:self.isAdded forKey:kONISOObjectAdd];
    
    
    
}

- (id)copyWithZone:(NSZone *)zone
{
    ONISOObjects *copy = [[ONISOObjects alloc] init];
    
    if (copy) {

        copy.creationDate = [self.creationDate copyWithZone:zone];
        copy.creationName = [self.creationName copyWithZone:zone];
        copy.objectsIdentifier = self.objectsIdentifier;
        copy.name = [self.name copyWithZone:zone];
        copy.objectsDescription = [self.objectsDescription copyWithZone:zone];
        copy.hasChild =self.hasChild;
    }
    
    return copy;
}


@end
